<template>
	<div>
		<el-dialog title="新建课程" :visible.sync="dialogVisible" width="1000px" @closed="resetForm('form')" :close-on-click-modal="false">
			<div>
				<el-form ref="form" :rules="rules" :model="form" label-width="110px">
					<el-row :gutter="20">
						<CoverImg :defaultValue="coverImgInfo" v-if="dialogVisible" :type="$store.getters.libraryType" @e-coverImgId="setCoverImgId" />
						<el-col :span="14">
							<el-form-item label="课程名称" prop="title">
								<el-input v-model.trim="form.title"></el-input>
							</el-form-item>

							<el-form-item label="课程编码">
								<el-input v-model.trim="form.code"></el-input>
							</el-form-item>
						</el-col>
						<!-- <el-col :span="14">
						  <el-form-item label="建设单位" prop="sourceUnit">
							<UnitSelect
							  v-if="dialogVisible"
							  :defaultValue="defaultDept"
							  @e-unitData="setUnitId"
							/>
						  </el-form-item>
						</el-col> -->
						<el-col :span="14">
							<el-form-item label="建设单位" prop="extFiled2">
								<el-input v-model.trim="form.extFiled2"></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="14" v-if="pxccObj.isShow == 0">
							<el-form-item :label="pxccObj.name || '培训层次'" prop="trainingLevel" v-if="$store.getters.libraryType == 1">
								<TrainingSelect :defaultValue="form.trainingLevel" @e-trainData="setTrainId" />
							</el-form-item>
						</el-col>
						<el-col :span="14" v-if="zyflObj.isShow == 0">
							<el-form-item :label="zyflObj.name || '资源分类'" prop="labelValue">
								<LabelSelect :type="$store.getters.libraryType" labelType="1" :defaultValue="form.labelValue" @e-labelData="setLabelId" />
							</el-form-item>
						</el-col>
					</el-row>
					<el-form-item label="简介" prop="content">
						<UEditor ref="ueditor" :defaultMsg="form.content" />
					</el-form-item>
				</el-form>
			</div>
			<span slot="footer" class="dialog-footer">
				<el-button type="custom" v-preventReClick @click="submitForm('form')">提 交</el-button>
				<el-button @click="dialogVisible = false">取 消</el-button>
			</span>
		</el-dialog>
	</div>
</template>
<script>
import { sysCourseAdd, sysCourseUpdate } from '@/api/admin/courseLibrary/sysCourse.js';
import courseCreate from '@/mixins/courseCreate';
import homeConfig from '@/mixins/homeConfig';
import Bus from '@/utils/bus.js';
export default {
	mixins: [courseCreate, homeConfig],
	components: {
		TrainingSelect: () => import('@/components/trainingSelect'),
		UnitSelect: () => import('@/components/unitSelect'),
		LabelSelect: () => import('@/components/labelSelect'),
		CourseTitleSelect: () => import('@/components/courseTitleSelect'),
		CoverImg: () => import('@/components/coverImg')
	},
	data() {
		return {
			title: '新增',
			dialogVisible: false,
			addOrEdit: 1,
			checkedData: {},
			coverImgInfo: null,
			form: {
				courseType: this.$store.getters.libraryType, //课程类型;1-课程库，2-专业库，3-兵种库，4-专题库，5-示范微视频，6-培训资源
				title: '',
				sourceUnit: '',
				content: '',
				fileId: '',
				extFiled2: '', //校外资源建设单位
				sourceType: 2, //1-校内，2-校外
				labelType: this.$store.getters.libraryType,
				labelValue: '',
				code: '', //课程编号(校内，code传引用课本源ID)
				trainingLevel: '', //培训层次
				auditStatus: 2, //审核状态：1-待审核，2-审核通过，3-不通过
				status: 2, //课程状态：0-未发布，1-已发布
				sysCourseTeamList: [{ auditStatus: 2, userId: this.$store.getters.userId, orderBy: 10 }]
			},
			rules: {
				code: [{ required: true, message: '请选择标题', trigger: 'blur' }],
				title: [{ required: true, message: '请输入课程名称', trigger: 'blur' }],
				sourceUnit: [{ required: true, message: '请选择建设单位', trigger: 'blur' }],
				extFiled2: [{ required: true, message: '请输入建设单位', trigger: 'blur' }],
				trainingLevel: [{ required: true, message: '请选择培训层次', trigger: 'blur' }],
				labelValue: [{ required: true, message: '请选择课程分类', trigger: 'blur' }]
			},
			schoolInfo: {},
			// zylxObj: {}, // 资源类型
			zyflObj: {}, // 资源分类
			pxccObj: {} // 培训层次
			// tgdwObj: {}, // 提供单位
		};
	},
	props: ['sourceType'],
	computed: {
		libraryTitle: function () {
			let libraryMeta = this.$store.getters.libraryMeta;
			return libraryMeta ? libraryMeta.title : '';
		},
		defaultDept: function () {
			return this.addOrEdit == 1 ? this.$store.getters.deptId : this.form.sourceUnit;
		}
	},
	mounted() {
		// this.zylxObj = this.showModuleObj('zylx')
		this.zyflObj = this.showModuleObj('zyfl');
		this.pxccObj = this.showModuleObj('pxcc');
		// this.tgdwObj = this.showModuleObj('tgdw')
		// console.log(this.zyflObj)
		Bus.$on('showDialogCourseAdd', (v) => {
			this.coverImgInfo = null;
			if (v) {
				this.checkedData = v;
				this.handleAddOrEdit(2);
			} else {
				this.form.sourceUnit = this.defaultDept;
				this.handleAddOrEdit(1);
			}
		});
	},
	beforeDestroy() {
		Bus.$off('showDialogCourseAdd');
	},
	methods: {
		setCourseTitle(v) {
			this.checkCourseTitle(v.id);
			this.form.code = v.id || '';
			this.schoolInfo = v || {};
		},
		setTrainId(v) {
			this.form.trainingLevel = v || '';
		},
		setUnitId(v) {
			this.form.sourceUnit = v || '';
		},
		setLabelId(v) {
			this.form.labelValue = v;
		},
		setCoverImgId(v) {
			this.form.fileId = v;
		},
		//显示弹窗
		handleAddOrEdit(type) {
			if (type == 2) {
				this.title = '编辑';
				for (let i in this.checkedData) {
					if (this.form.hasOwnProperty(i)) {
						this.form[i] = this.checkedData[i] || '';
					}
				}
				this.coverImgInfo = this.checkedData.hasOwnProperty('sysAttachment') ? this.checkedData.sysAttachment : '';
				this.form.id = this.checkedData.id;
			} else {
				this.title = '新增';
				delete this.form.id;
			}
			this.addOrEdit = type;
			this.dialogVisible = true;
		},
		submitForm(form) {
			this.form.sourceType = this.sourceType;
			this.form.content = this.$refs.ueditor.message;
			this.form.labelValue = this.form.labelValue.toString();
			this.form.courseType = this.$store.getters.libraryType;
			this.form.labelType = this.$store.getters.libraryType;
			this.$refs[form].validate((valid) => {
				if (valid) {
					this.addOrEdit == 1 ? this.add() : this.edit();
				}
			});
		},
		//新增
		add() {
			sysCourseAdd(this.form).then((res) => {
				this.dialogVisible = false;
				Bus.$emit('refreshCourseList');
				this.$message({
					message: '操作成功',
					type: 'success'
				});
			});
		},
		//编辑
		edit() {
			sysCourseUpdate(this.form).then((res) => {
				this.dialogVisible = false;
				Bus.$emit('refreshCourseList');
				this.$message({
					message: '操作成功',
					type: 'success'
				});
			});
		},
		//重置表单
		resetForm(form) {
			this.$refs.ueditor.message = '';
			this.$refs[form].resetFields();
			for (let key in this.form) {
				if (key != 'sysCourseTeamList') {
					this.form[key] = '';
				}
			}
			this.form.courseType = 1;
			this.form.auditStatus = 1;
			this.form.labelType = 1;
			this.form.status = 0;
			this.schoolInfo = {};
		}
	}
	// 校本：建设单位，默认账号登录人的单位（截取到校区），允许更换单位。前端显示该建设单位。
	// 校外：资源引进单位（默认账号登录人的单位，但允许换单位），资源建设单位（手填）。前端显示资源建设单位。
	// 资源统计时，如果是校本资源使用建设单位，如果是校外使用引进单位进行统计。
};
</script>
